package demo2;
import java.util.Stack;

class MyQueue {
    public Stack<Integer> s1;//入栈
    public Stack<Integer> s2;//出栈
    public MyQueue() {
        this.s1 = new Stack<>();
        this.s2 = new Stack<>();
    }
    //进队
    public void push(int x) {
        s1.push(x);
    }
    //出队
    public int pop() {
        if(empty()) {
            return -1;
        }
        //s2如同蓄水池，一旦要出队列，就全压s1
        //前提一定要是s2为空！若不为空，再压入s2，顺序就不一样！
        if(s2.empty()){
            while(!s1.empty()) {
                s2.push(s1.pop());
            }
            return s2.pop();
        }
        //如果s2里有元素，就直接弹出
        return s2.pop();
    }
    public int peek() {
        if(empty()) {
            return -1;
        }
        //s2如同蓄水池，一旦要出队列，就全压s1
        //前提一定要是s2为空！若不为空，再压入s2，顺序就不一样！
        if(s2.empty()){
            while(!s1.empty()) {
                s2.push(s1.pop());
            }
            return s2.peek();
        }
        //如果s2里有元素，就直接弹出
        return s2.peek();
    }
    
    public boolean empty() {
        return s1.empty() && s2.empty();

    }
}